package com.xoa.service.hr.hrEvaluate;

import com.xoa.dao.hr.hrEvaluate.HrEvaluateMapper;
import com.xoa.dao.users.UsersMapper;

import com.xoa.model.hr.hrEvaluate.HrEvaluate;
import com.xoa.model.hr.hrEvaluate.HrEvaluatePrivWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.LimsJson;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by qiyang on 2019/11/13.
 */
@Service
public class HrEvaluateService {

    @Autowired
    private HrEvaluateMapper hrEvaluateMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private HrEvaluatePrivService hrEvaluatePrivService;

    /**
     * 查看最新评价
     */
    public LimsJson selectByhrEvaluateNew(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag, Map map) {
        LimsJson toJson = new LimsJson(1, "暂无数据！");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

            String deptId = request.getParameter("deptId");
            String evLevel = request.getParameter("evLevel");

            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(useFlag);
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            map.put("page", pageParams);
            map.put("deptId", deptId);
            map.put("evLevel", evLevel);

            //判断当前登录人是否有权限查看 那些人
            Map map1 = new HashMap();
            map1.put("viewOrEvaluateUsers", user.getUserId());
            map1.put("privType", '1');
            map.put("priv",hrEvaluatePrivService.isUserPriv(map1));


            List<HrEvaluate> hrEvaluates = hrEvaluateMapper.selectByHrEvaluate(map);
            if (hrEvaluates != null && hrEvaluates.size() > 0) {
                for (HrEvaluate hrEvaluate:hrEvaluates) {
                    String userId = hrEvaluate.getUserId();
                    String username = usersMapper.getUsernameByUserId(userId);
                    hrEvaluate.setUserName(username);
                    String userIdEv = hrEvaluate.getEvUser();
                    String usernameEv = usersMapper.getUsernameByUserId(userIdEv);
                    hrEvaluate.setEvUserName(usernameEv);
                }
                toJson.setCode(0);
                toJson.setMsg("查询成功");
                toJson.setCount(pageParams.getTotal());
                toJson.setData(hrEvaluates);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 历史评价
     */
    public LimsJson selectByUserId(HttpServletRequest request, Integer page, Integer pageSize,Map map) {
        LimsJson toJson = new LimsJson(1, "err");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

            PageParams pageParams = new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(pageSize);
            map.put("page", pageParams);

            String uId = request.getParameter("userId");

            map.put("userId", uId);

            //判断是否有权限  控制数据不控制按钮
         /*   Map map1 = new HashMap();
            map1.put("privType", '1');
            map1.put("viewOrEvaluateUsers", user.getUserId());
            map1.put("privUsers", uId);

            if (!hrEvaluatePrivService.isPriv(map1)){
                toJson.setMsg("无权限查看");
                return toJson;
            }*/


            List<HrEvaluate> hrEvaluates = hrEvaluateMapper.selectByUserId(map);
            if (hrEvaluates != null && hrEvaluates.size() > 0) {
                for (HrEvaluate hrEvaluate:hrEvaluates) {
                    String userId = hrEvaluate.getUserId();
                    String username = usersMapper.getUsernameByUserId(userId);
                    hrEvaluate.setUserName(username);
                    String userIdEv = hrEvaluate.getEvUser();
                    String usernameEv = usersMapper.getUsernameByUserId(userIdEv);
                    hrEvaluate.setEvUserName(usernameEv);
                }
                toJson.setCode(0);
                toJson.setMsg("查询成功");
                toJson.setCount(pageParams.getTotal());
                toJson.setData(hrEvaluates);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toJson;
    }


    public ToJson<HrEvaluate> queryHrEvaluates(HttpServletRequest request, String userId) {
        ToJson<HrEvaluate> toJson = new ToJson<>(1,"err");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);

            Map map = new HashMap();
            map.put("evUser",user.getUserId());
            map.put("userId",userId);

            List<HrEvaluate> hrEvaluateList = hrEvaluateMapper.selectHrEvaluatesByUserId(map);
            toJson.setObj(hrEvaluateList);
            toJson.setFlag(0);
            toJson.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
        }
        return toJson;
    }

    public ToJson<HrEvaluate> addHrEvaluates(HttpServletRequest request, HrEvaluate hrEvaluate) {
        ToJson<HrEvaluate> toJson = new ToJson<>(1,"err");
        try {

            String evUser = request.getSession().getAttribute("userId").toString();
            if(StringUtils.checkNull(evUser)) {
                toJson.setMsg("login_err");
                return toJson;
            }
            hrEvaluate.setEvUser(request.getSession().getAttribute("userId").toString());
            hrEvaluate.setEvTime(DateFormat.getStrDate(new Date()));
            int selective = hrEvaluateMapper.insertSelective(hrEvaluate);
            if(selective > 0) {
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return toJson;
    }
}
